package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.app.AppChannelVO;
import com.qumitech.visualnovel.controller.vo.app.AppPackageVO;
import com.qumitech.visualnovel.service.app.AppChannelService;
import com.qumitech.visualnovel.service.app.AppPackageService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/3/12 11:43
 */

@RestController
@RequestMapping("/api/app")
public class AppController {

    @Autowired
    private AppPackageService appPackageService;
    @Autowired
    private AppChannelService appChannelService;


    @GetMapping(value = "/package")
    public BaseRsp getBlockPlan(@RequestParam(value = "offset", defaultValue = "0") Integer offset,
                                @RequestParam(value = "limit", defaultValue = "20") Integer limit,
                                @RequestParam(value = "package_name", required = false) String packageName,
                                @RequestParam(value = "package_id", required = false) String packageId

    ) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("package", appPackageService.listAppPackageVO(offset, limit, packageId, packageName)));
    }

    @PostMapping(value = "/package")
    public BaseRsp addButtonConfig(@RequestBody AppPackageVO appPackageVO) {
        appPackageService.addAppPackage(appPackageVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping(value = "/package/{package_id}")
    public BaseRsp updateButtonConfig(@RequestBody AppPackageVO packageVO, @PathVariable(name = "package_id") String packageId) {
        packageVO.setPackageId(packageId);
        appPackageService.updateAppPackage(packageVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping(value = "/package/{package_id}")
    public BaseRsp removeButtonConfig(@PathVariable(name = "package_id") String packageId) {
        appPackageService.removeAppPackage(packageId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }


    @PostMapping(value = "/channel")
    public BaseRsp addAppChannel(@RequestBody AppChannelVO appChannelVO) {
        appChannelService.addAppChannel(appChannelVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping(value = "/channel/{channel_id}")
    public BaseRsp removeAppChannel(@PathVariable(name = "channel_id") String channelId) {
        appChannelService.removeAppChannel(channelId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping(value = "/channel/{channel_id}")
    public BaseRsp updateAppChannel(@RequestBody AppChannelVO appChannelVO, @PathVariable(name = "channel_id") String channelId) {
        appChannelVO.setChannelId(channelId);
        appChannelService.updateAppChannel(appChannelVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }


    @GetMapping(value = "/channel")
    public BaseRsp listAppChannel(@RequestParam(value = "offset", defaultValue = "0") Integer offset,
                                  @RequestParam(value = "limit", defaultValue = "20") Integer limit,
                                  @RequestParam(value = "device_type", required = false) Integer  deviceType) {

        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("channel_list", appChannelService.listAppChannelVO(offset, limit,deviceType)));
    }


}
